#include "comm.h"
#include <map>

#ifdef WIN32
#include <windows.h>
#include <gdiplus.h>
#endif

std::map<std::string, DuiUIFunc> g_dui_comm_func_map = {};
std::map<std::string, void *> g_dui_comm_font_map = {};
std::map<std::string, void *> g_dui_comm_image_map = {};

void dui_comm_register_func(const char *name, DuiUIFunc func)
{
    g_dui_comm_func_map[name] = func;
}

void dui_comm_unregister_func(const char *name)
{
    g_dui_comm_func_map.erase(name);
}

DuiUIFunc dui_comm_get_func(const char *name)
{
    if (g_dui_comm_func_map.find(name) == g_dui_comm_func_map.end())
    {
        return nullptr;
    }
    return g_dui_comm_func_map[name];
}

void *dui_comm_font_get(std::string name)
{
    if (g_dui_comm_font_map.find(name) == g_dui_comm_font_map.end())
    {
        wchar_t name2[1024];
        MultiByteToWideChar(CP_UTF8, 0, name.c_str(), -1, name2, 1024);
        // 加载字体
        auto fontFace = new Gdiplus::FontFamily(name2);
        g_dui_comm_font_map[name] = fontFace;
    }
    return g_dui_comm_font_map[name];
}

void *dui_comm_image_get(std::string name)
{
    if (g_dui_comm_image_map.find(name) == g_dui_comm_image_map.end())
    {
         wchar_t name2[1024];
        MultiByteToWideChar(CP_UTF8, 0, name.c_str(), -1, name2, 1024);
        auto image = new Gdiplus::Image(name2);
        g_dui_comm_image_map[name] = image;
    }
    return g_dui_comm_image_map[name];
}

bool dui_comm_pt_in_rect(int x, int y, DuiRect rc)
{
    return x >= rc.x && x <= rc.x + rc.w && y >= rc.y && y <= rc.y + rc.h;
}
